﻿using MapHello.Core;
using MapHello.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MapHello.Service
{
    public class MapHelloService
    {
        public void FakeConfiguration()
        {
            this.SaveConfig(Config.MyNavigationPages, "Map|Settings|Friends|Posts");
        }

        public void LoadLivePerson(LoadPersonDelegate del, LoadPersonContext context)
        {
            LoadLivePersonTask task = new LoadLivePersonTask();
            LoadPersonDelegate completionDelegate = delegate(LoadLivePersonArgs args)
            {
                if (null != del)
                {
                    args.Person.IsMe = args.IsMe;
                    del(args);
                }
            };
            task.OnTaskCompleted += completionDelegate;
            task.State = context;
            TaskManagement.RunTask(task);
        }

        public void StartSystemTask()
        {
            if (!TaskManagement.IsSystemTaskActive)
            {
                Debug.WriteLine("Starting system task...");
                SystemTask task = new SystemTask();
                TaskManagement.IsSystemTaskActive = true;
                TaskManagement.RunTask(task);
                Debug.WriteLine("System task started!");
            }
        }

        public SecurityContext LoadSecurityContext()
        {
            var tokenConfig = this.LoadConfig(Config.WindowsLiveToken);
            var tokenExpiredConfig = this.LoadConfig(Config.WindowsLiveTokenExpired);

            SecurityContext result = null;

            if (null != tokenConfig)
            {
                result = new SecurityContext()
                {
                    Token = tokenConfig.Value,
                    TokenExpired = DateTime.Parse(tokenExpiredConfig.Value)
                };
            }
            return result;
        }

        public void SaveSecurityContext(SecurityContext context)
        {
            if (null != context)
            {
                this.SaveConfig(Config.WindowsLiveToken, context.Token, true);
                this.SaveConfig(Config.WindowsLiveTokenExpired, context.TokenExpired.ToString(), true);
            }
        }

        public void SaveConfig(string name, string value, bool isRoaming = false)
        {
            var context = new MapHelloDataContext();
            var result = context.Configs.FirstOrDefault<Config>(i => i.Name == name);
            if (null != result)
            {
                result.Value = value;
            }
            else
            {
                result = new Config()
                {
                    Name = name,
                    Value = value,
                    IsRoaming = isRoaming
                };
                context.Configs.InsertOnSubmit(result);
            }

            context.SubmitChanges();

            if (isRoaming)
            {
                var task = new RoamingConfigTask();
                task.State = result;
                TaskManagement.RunTask(task);
            }
        }

        public Config LoadConfig(string name)
        {
            var context = new MapHelloDataContext();
            var result = context.Configs.FirstOrDefault<Config>(i => i.Name == name);

            return result;
        }

        public Person LoadPerson(string personId)
        {
            var context = new MapHelloDataContext();
            var result = context.Persons.FirstOrDefault<Person>(i => i.Id == personId);

            return result;
        }

        public Person LoadMe()
        {
            var context = new MapHelloDataContext();
            var result = context.Persons.FirstOrDefault<Person>(i => i.IsMe);

            return result;
        }

        public void SavePerson(Person person)
        {
            var context = new MapHelloDataContext();
            var result = context.Persons.FirstOrDefault<Person>(i => i.Id == person.Id);
            if (null != result)
            {
                result.Name = person.Name;
                result.Position = person.Position;
                result.IsMe = person.IsMe;
                result.ImageUrl = person.ImageUrl;
                result.ImageBinary = person.ImageBinary;
                result.Type = person.Type;
            }
            else
            {
                Person p = new Person()
                {
                    Id = person.Id,
                    Name = person.Name,
                    IsMe = person.IsMe,
                    ImageUrl = person.ImageUrl,
                    ImageBinary = person.ImageBinary,
                    Type = person.Type,
                    Position = person.Position
                };
                context.Persons.InsertOnSubmit(p);
            }

            context.SubmitChanges();
        }

        public void InitializeUserData()
        {

        }
    }
}
